﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using Newtonsoft.Json;
using LiuYan.Service;
using LiuYan.Web.Controllers;
namespace LiuYan.Web.Areas.Mall.Controllers
{
    public class MessageController : Controller
    {
        //
        // GET: /Mall/Message/

        public ActionResult Index()
        {
            //修改订单状态
            if (Request.HttpMethod == "POST")
            {
                StreamReader reader = new StreamReader(Request.InputStream); //获取微信post到该页面的状态链接
                string xmlData = reader.ReadToEnd();
                XmlDocument doc = new XmlDocument(); //读取xml数据
                doc.LoadXml(xmlData);
                XmlElement root = doc.DocumentElement;
                int len1 = root.ChildNodes.Count;
                var out_trade_no = "";
                for (int i = 0; i < len1; i++)
                {
                    string key1 = root.ChildNodes[i].Name;
                    string value = root.ChildNodes[i].InnerText.Trim();
                    if (key1 == "out_trade_no")
                    {
                        out_trade_no = value;
                    }
                }
               
              
                 if (out_trade_no.Contains("sp"))//修改商品订单
                {
                    WriteMess("开始修改订单");
                   var row= new OrderService().EditOrderStatus(out_trade_no, 2);
                   WriteMess("修改订单：" + row);
                }
               
            }
            return View();
        }
        public void WriteMess(string mess)
        {

            string realPath = Server.MapPath("//rizhi.txt");//真正的地址
            FileStream fsWriter = null;//公开以文件为主的流
            if (!System.IO.File.Exists(realPath))
            {
                fsWriter = new FileStream(realPath, FileMode.Create);
            }
            else
            {
                fsWriter = new FileStream(realPath, FileMode.Append);
            }
            StreamWriter sw = new StreamWriter(fsWriter);//实现一个类，向流中写入字符
            sw.WriteLine(mess);//写入流
            sw.Close();//类关闭
            fsWriter.Close();//流关闭
        }

    }
}
